package com.weipai.springboot.module.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Shop;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.FileUploadService;
import com.weipai.springboot.module.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/07/27 17:51
 * 店铺管理类
 */
@RestController
public class ShopController {
    
    @Autowired
    ShopService shopService;
    @Autowired
    FileUploadService fileUploadService;

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页条数
     * @param draw 前台参数，照样返回
     * @param search 搜索，如果无搜索此值为paging
     * @param sort time
     * @param way desc，asc，noway
     */
    @GetMapping("/shop/list/{page}/{size}/{draw}/{search}/{sort}/{way}/{applicantStatus}")
    public Map<String, Object> list(@PathVariable("page") Integer page,
                                    @PathVariable("size") Integer size,
                                    @PathVariable("draw") String draw,
                                    @PathVariable("search") String search,
                                    @PathVariable("sort") String sort,
                                    @PathVariable("way") String way,
                                    @PathVariable("applicantStatus") Integer applicantStatus){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page, size);
        List<Shop> list = shopService.selectAllByShopDel(0,search, sort, way, applicantStatus);
        PageInfo<Shop> pageInfo = new PageInfo<>(list);
        if(list.size()>0){
            map.put("pageData", pageInfo.getList());
            map.put("recordsTotal", pageInfo.getTotal());
            map.put("draw", draw);
            map.put("pages", pageInfo.getPages());
            map.put("code",200);
        }else{
            map.put("code",401);
            map.put("msg","暂无数据");
        }
        return map;
    }

    /**
     * 查询店铺详情
     * @param shopId:店铺id
     * @return
     */
    @RequestMapping("/shop/getShopById")
    public Response getShopById( String shopId){
        return shopService.getShopById(shopId);
    }


    /**
     * 更新   店铺信息详情
     * @param shop:店铺
     * @return
     */
    @RequestMapping("/shop/updateShopById")
    public Response updateShopById(Shop shop){
        Response response = new Response();
        response =shopService.updateShopById(shop);
        return response;
    }


    /**
     * 删除
     * @param shopId:店铺的id
     * @return
     */
    @DeleteMapping("/shop/deleteById/{id}")
    public Response deleteById(@PathVariable("id")  String shopId){
        return shopService.deleteById(shopId);
    }



    /**
     * 物理删除 店铺----直接从表中彻底删除 shop
     * @param shopId:店铺的id
     * @return
     */
    @DeleteMapping("/shop/deleteByShopId/{shopId}")
    public Response deleteByShopId(@PathVariable("id")  String shopId){
        return shopService.deleteByShopId(shopId);
    }



    /**
     * 批量删除
     * @param id:店铺的id集合
     * @return
     */
    @DeleteMapping("/shop/batchDeleteById/{id}")
    public Response batchDeleteById(@PathVariable("id")  String[] id){
        return shopService.batchDeleteById(id);
    }
    
    /**
     * 更改店铺的状态--启用/禁用
     * @param shopId:店铺的id
     * @return
     */
    @GetMapping("/shop/changeStatus/{id}")
    public Response changeStatus(@PathVariable("id")  String shopId){
        return shopService.changeStatus(shopId);
    }

    /**
     * 店铺的审核
     * @param id：店铺id
     * @param applicantStatus：状态1：通过， 2：驳回  0: 等待审核
     *            rejectionReason   被驳回理由
     * @return
     */
    @GetMapping("/shop/audit/{id}/{rejectionReason}/{applicantStatus}")
    public Response audit(@PathVariable("id") String id,
                          @PathVariable("rejectionReason") String rejectionReason,
                          @PathVariable("applicantStatus") String applicantStatus){
        return shopService.audit(id,rejectionReason,applicantStatus);
    }

    /**
     * 绑定手机号
     * @param shopId
     * @param mobile
     * @param cold
     * @param oldCode
     * @return
     */
    @PostMapping("/shop/updateMobile")
    public Response updateMobile(String shopId,String mobile,String cold,String oldCode){
        return shopService.updateMobile(shopId,mobile,cold,oldCode);
    }

    /**
     * 修改店铺邮箱
     * @param shopId
     * @param email
     * @return
     */
    @PostMapping("/shop/updateEmail")
    public Response updateEmail(String shopId,String email){
        return shopService.updateEmail(shopId,email);
    }


    /**
     * 前端商品详情页的店铺的关注人数、全部商品、店铺名称、logo信息
     * @param shopId
     * @return
     */
    @GetMapping("/shop/getShopHeaderInfo/{shopId}")
    public Response getShopHeaderInfoByShopId(@PathVariable("shopId") String shopId){
        return shopService.getShopHeaderInfoByShopId(shopId);
    }
    
    /**
     * 更改客户的未结算值和已结算值
     * @param shopId
     * @param noWithdrawal
     * @param haveWithdrawal
     * @return
     */
    @GetMapping("/shop/changeWithdrawal/{shopId}/{noWithdrawal}/{haveWithdrawal}")
    public Response changeWithdrawal(@PathVariable("shopId") String shopId,
                                     @PathVariable("noWithdrawal") String noWithdrawal,
                                     @PathVariable("haveWithdrawal") String haveWithdrawal){
        return shopService.changeWithdrawal(shopId, noWithdrawal, haveWithdrawal);
    }
    
}
